package io2.bio;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Scanner;

public class BioClient {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket();
        socket.connect(new InetSocketAddress("127.0.0.1", 8080));
        System.out.println("连接成功");
        System.out.println("请输入：");
        Scanner scanner = new Scanner(System.in);

        BufferedWriter writer = null;

        try {
            writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            writer.write(scanner.nextLine());
            writer.write("\r\n");
            writer.write("\r\n");
            writer.flush();
            System.out.println("数据发送完毕");
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            } catch (IOException e) {
                e.printStackTrace();
            }
            String str;
            try {
                while ((str = reader.readLine()) != null)
                    System.out.println("收到数据：" + str);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if(reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            System.out.println("数据接收完毕");
        } finally {
            if(writer != null)
                writer.close();
            socket.close();
        }
    }
}
